package com.example.ch02_student;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.ch02_student.adminsDo.AdminsDo;
import com.example.ch02_student.entity.RolePremissionVo;
import com.example.ch02_student.entity.Teacher;
import com.example.ch02_student.utils.RoleInfo;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TeacherMain extends AppCompatActivity {
    private EditText no, name, sex1, tel1, age1, email, yz,time;//验证码
    private ImageView img;
    private OkHttpClient okHttpClient;
    private static String tno,tname,timg,sex,age,tel,createTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_main);
        init();
        //修改身份表示
        RoleInfo.INT_ROLE=2;


        okHttpClient=new OkHttpClient();
        Intent intent = getIntent();
        Teacher teacher =(Teacher) intent.getSerializableExtra("teacher");
        if (teacher!=null){
            if (teacher.getImg()!=null){
                //赋值给静态变量
                timg=teacher.getImg();
                img.setImageResource(Integer.parseInt(teacher.getImg()));
            }
            tno=teacher.getTno();
            tname=teacher.getTname();
            sex=teacher.getSex();
            age=teacher.getAge()+"";
            tel=teacher.getTel();
            createTime=teacher.getCreateTime()+"";
            xuanran();
            //权限查询 设置到静态变量中
            Request build = new Request.Builder().url(UrlBase.URL_BASE + "/admins/selectPerById?no="+tno).build();
            Call call = okHttpClient.newCall(build);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                }
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String string = response.body().string();
                    RolePremissionVo[] rolePremissionVo = new Gson().fromJson(string, RolePremissionVo[].class);
                    List<String> list = rolePremissionVo[0].getList();
                    StringBuilder stringBuilder = new StringBuilder();

                    for (String s : list) {
                        stringBuilder.append(s+",");
                    }
                    RoleInfo.PERSSIONS=stringBuilder+"";
                }
            });


        }


    }

    public void goEmail(View view) {

    }

    //更新
    public void goUpdate(View view) {

    }

    //跳转操作界面
    public void teacherdo(View view) {
        startActivity(new Intent(TeacherMain.this,AdminsDo.class));
    }

    //初始化控件
    public void init() {
        img=findViewById(R.id.img);
        no=findViewById(R.id.no);
        name=findViewById(R.id.name);
        sex1=findViewById(R.id.sex);
        tel1=findViewById(R.id.tel);
        age1=findViewById(R.id.age);
        email=findViewById(R.id.email);
        yz=findViewById(R.id.yz);//验证码
        time=findViewById(R.id.createTime);

    }
    //渲染
    public void xuanran(){
        no.setText(tno);
        name.setText(tname);
        sex1.setText(sex);
        age1.setText(age+"");
        tel1.setText(tel);
        time.setText(createTime);
    }
}
